import java.util.Random;

public class BubbleSort {
//    传的是地址
    public void bubSort(int[] arr) {
        int i = 0;
        while (i + 1 < arr.length) {
            if (arr[i] <= arr[++i]) continue;

            else {
                for (int j = i - 1; j + 1 < arr.length; j++) {
//                arr[i] > arr[i + 1] &&
                    if (arr[j] > arr[j + 1]) {
                        int temp = arr[j + 1];
                        arr[j + 1] = arr[j];
                        arr[j] = temp;
                    } else {
                        break;
                    }
                }
                i = 0;
            }
        }
    }

    public void bubSortBetter(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            for (int j = 0; j < arr.length - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }

    public static void main(String[] args) {
        BubbleSort bs = new BubbleSort();
        Random r = new Random();
        int size = 5;
        int[] arr = new int[size];
        for (int i = 0; i < size; i++) {
            arr[i] = r.nextInt(0, 100);
        }
//        bs.BubSort(arr);
        bs.bubSortBetter(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
